/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.SetuCompBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.SetuBillDao;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.Establishment;
import com.minierp.model.SetuBill;
import com.minierp.service.SetuBillService;
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class SetuCompFormCtr extends SimpleFormController {
	private SetuBillDao setuBillDao;
	private IdGenerator idGenerator;
	private SetuBillService setuBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		SetuBill sb = (SetuBill) this.setuBillDao.get(SetuBill.class,
				Long.parseLong(id));
		BusinessBill bb = (BusinessBill) this.setuBillDao.get(
				BusinessBill.class, sb.getBbId());
		Establishment est = (Establishment) this.setuBillDao.get(
				Establishment.class, bb.getClientId());
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sfHour = new SimpleDateFormat("HH");
		SimpleDateFormat sfMinute = new SimpleDateFormat("mm");
		SimpleDateFormat sfLong = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date now = new Date();
		SetuCompBean form = new SetuCompBean();
		form.setBbCode(bb.getBillCode());
		form.setBillCode(sb.getBillCode());
		form.setBillDateStr(sfLong.format(sb.getBillDate()));
		form.setCompDateStr(sf.format(now));
		form.setCompDateHour(sfHour.format(now));
		form.setCompDateMinu(sfMinute.format(now));
		form.setCompImg(sb.getCompImg());
		form.setCostStr(sb.getCost().toString());
		form.setEstName(est.getEstName());
		form.setSetuIdStr(sb.getSetuIdStr());
		form.setSetuNameStr(sb.getSetuNameStr());
		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		SetuCompBean form = (SetuCompBean) command;
		if (!EncCommonHelper.checkPrivilege(request, "SETU_COMP")) {
			throw new Exception("Permission deny!");
		} else {
			String errMsg = "";
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date realDate = null;

			try {
				realDate = sf
						.parse(form.getCompDateStr() + " "
								+ form.getCompDateHour() + ":"
								+ form.getCompDateMinu());
			} catch (Exception arg22) {
				errMsg = "完工日期填写错误！格式：2000-01-30";
			}

			BigDecimal cost = new BigDecimal(0);
			if (errMsg.equals("")) {
				try {
					cost = new BigDecimal(form.getCostStr());
				} catch (Exception arg21) {
					errMsg = "工程金额只能输入数字";
				}
			}

			if (errMsg.equals("")) {
				String compImg = "";
				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
				MultipartFile multipartFile = multipartRequest
						.getFile("comp_img");
				if (multipartFile != null && !multipartFile.isEmpty()) {
					String w = multipartFile.getOriginalFilename();
					String extendName = EncCommonHelper.getExtendName(w);
					String uploadPath = EncCommonHelper
							.getUploadVirtualPath(this.getServletContext());
					String realPath = this.getServletContext().getRealPath(
							uploadPath);
					String fileName = ub.getUser().getId() + "_"
							+ System.currentTimeMillis() + "." + extendName;
					FileCopyUtils.copy(multipartFile.getBytes(), new File(
							realPath + System.getProperty("file.separator")
									+ fileName));
					compImg = uploadPath + fileName;
				}

				try {
					this.setuBillService.txComp(Long.parseLong(id), realDate,
							form.getSetuIdStr(), form.getSetuNameStr(), cost,
							compImg, ub, this.setuBillDao, this.idGenerator);
					ModelAndView w1 = new ModelAndView(this.getSuccessView());
					w1.addObject("returnValue", id);
					EncCommonHelper.setAlertMessage(request, "保存成功！");
					return w1;
				} catch (CannotAcquireLockException arg19) {
					EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
					return this.showForm(request, errors, this.getFormView());
				} catch (NotAllowException arg20) {
					EncCommonHelper
							.setAlertMessage(request, arg20.getMessage());
					return this.showForm(request, errors, this.getFormView());
				}
			} else {
				EncCommonHelper.setAlertMessage(request, errMsg);
				return this.showForm(request, errors, this.getFormView());
			}
		}
	}

	public SetuBillDao getSetuBillDao() {
		return this.setuBillDao;
	}

	public void setSetuBillDao(SetuBillDao setuBillDao) {
		this.setuBillDao = setuBillDao;
	}

	public SetuBillService getSetuBillService() {
		return this.setuBillService;
	}

	public void setSetuBillService(SetuBillService setuBillService) {
		this.setuBillService = setuBillService;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}
}